# Copyright (c) 2021 Presto Labs Pte. Ltd.
# Author: gekim

import requests
from coin2.exchange.product_info import ProductInfoDefault
from coin2.exchange.symbology import SymbologyInfo


def generate():
  mea = 'Futures.Okex.v5-swap'
  me = 'Futures.Okex'
  default = ProductInfoDefault()
  symbol_info = SymbologyInfo()
  data = requests.get('https://www.okx.com/api/v5/public/instruments?instType=SWAP').json()["data"]

  pis = []
  for info in data:
    if "state" not in info or info["state"] != "live":
      continue

    native_symbol = info['instId']
    uly_parts = info['instFamily'].split("-")
    native_base = uly_parts[0]
    native_quote = uly_parts[1]
    base = symbol_info.native_to_norm_currency(mea, native_base)
    quote = symbol_info.native_to_norm_currency(mea, native_quote)
    symbol = "%s-%s.PERPETUAL" % (base, quote)

    pi = dict(
        symbol=symbol,
        base=base,
        quote=quote,
        native_symbol=native_symbol,
        native_base=native_base,
        native_quote=native_quote,
        price_ticksize=float(info['tickSz']),
        qty_ticksize=float(info['lotSz']),
        is_inverse=(info['ctType'] == 'inverse'),
        contract_value=float(info['ctVal']),
    )
    pi = {**pi, **default.product_info(me, symbol)}
    pis.append(pi)

  pis = sorted(pis, key=lambda x: x['symbol'])
  return pis


if __name__ == "__main__":
  print(generate())
